import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: {title: '个人理财管理系统', icon: 'el-icon-coffee-cup', roles: ['normal']}
    }]
  },

  {
    path: '/user',
    component: Layout,
    children: [{
      path: 'center',
      name: 'UserCenter',
      component: () => import('@/views/users/center.vue'),
      meta: {title: '个人中心', icon: 'el-icon-coffee-cup'},
      hidden: true
    }]
  },

  // 404 page must be placed at the end !!!
  //把这里的404路由添加到asyncRoutes，否则刷新页面被constantRoutes的404接管.
  /*
  {path: '*', redirect: '/404', hidden: true}*/
]


export const asyncRoutes = [
  {
    path: '/expense',
    component: Layout,
    redirect: '/expense/show',
    name: 'Expense',
    meta: {title: '支出管理', icon: 'el-icon-bank-card', roles: ['normal']},
    children: [
      {
        path: 'show',
        name: 'Show',
        component: () => import('@/views/expense/show.vue'),
        meta: {title: '支出展示', icon: 'el-icon-s-data', roles: ['normal']}
      },
      {
        path: 'list',
        name: 'List',
        component: () => import('@/views/expense/list.vue'),
        meta: {title: '支出列表', icon: 'el-icon-edit-outline', roles: ['normal']}
      },
      {
        path: 'add',
        name: 'Add',
        component: () => import('@/views/expense/add.vue'),
        meta: {title: '新增支出信息', icon: 'el-icon-s-data', roles: ['normal']},
        hidden: true
      },
      {
        path: 'details',
        name: 'Details',
        component: () => import('@/views/expense/details.vue'),
        meta: {title: '支出详情', icon: 'el-icon-s-data', roles: ['normal']},
        hidden: true
      }
    ]
  },

  {
    path: '/income',
    component: Layout,
    redirect: '/income/show',
    name: 'Income',
    meta: {title: '收入管理', icon: 'el-icon-money', roles: ['normal']},
    children: [
      {
        path: 'show',
        name: 'Show',
        component: () => import('@/views/income/show.vue'),
        meta: {title: '收入展示', icon: 'el-icon-s-data', roles: ['normal']}
      },
      {
        path: 'list',
        name: 'List',
        component: () => import('@/views/income/list.vue'),
        meta: {title: '收入列表', icon: 'el-icon-edit-outline', roles: ['normal']}
      },
      {
        path: 'add',
        name: 'Add',
        component: () => import('@/views/income/add.vue'),
        meta: {title: '新增收入信息', icon: 'el-icon-s-data', roles: ['normal']},
        hidden: true
      },
      {
        path: 'details',
        name: 'Details',
        component: () => import('@/views/income/details.vue'),
        meta: {title: '收入详情', icon: 'el-icon-s-data', roles: ['normal']},
        hidden: true
      }
    ]
  },

  {
    path: '/stock',
    component: Layout,
    redirect: '/stock/index',
    name: 'Stock',
    meta: {title: '股票投资', icon: 'el-icon-picture-outline', roles: ['normal']},
    children: [
      {
        path: 'index',
        name: 'Index',
        component: () => import('@/views/stock/index.vue'),
        meta: {title: '股指信息', icon: 'el-icon-top', roles: ['normal']}
      },
      {
        path: 'sector',
        name: 'Sector',
        component: () => import('@/views/stock/sector.vue'),
        meta: {title: '板块信息', icon: 'el-icon-s-grid', roles: ['normal']}
      },
      {
        path: 'mystock',
        name: 'MyStock',
        component: () => import('@/views/stock/myselect.vue'),
        meta: {title: '我的股票', icon: 'el-icon-star-on', roles: ['normal']}
      },
      {
        path: 'add',
        name: 'Add',
        component: () => import('@/views/stock/add.vue'),
        meta: {title: '添加自选股', icon: 'el-icon-star-on', roles: ['normal']},
        hidden: true
      },
      {
        path: 'details',
        name: 'Details',
        component: () => import('@/views/stock/details.vue'),
        meta: {title: '股票详情', icon: 'el-icon-star-on', roles: ['normal']},
        hidden: true
      }
    ]
  },

  {
    path: '/fund',
    component: Layout,
    redirect: '/fund/index',
    name: 'Fund',
    meta: {title: '基金投资', icon: 'el-icon-coin', roles: ['normal']},
    children: [
      {
        path: 'index',
        name: 'Index',
        component: () => import('@/views/fund/index.vue'),
        meta: {title: '基金排行', icon: 'el-icon-top', roles: ['normal']}
      },
      {
        path: 'details',
        name: 'Details',
        component: () => import('@/views/fund/details.vue'),
        meta: {title: '基金详情', icon: 'el-icon-s-grid', roles: ['normal']}
      },
      {
        path: 'myfund',
        name: 'MyFund',
        component: () => import('@/views/fund/myselect.vue'),
        meta: {title: '我的基金', icon: 'el-icon-star-on', roles: ['normal']}
      }
    ]
  },

  {
    path: '/assets',
    component: Layout,
    redirect: '/assets/show',
    name: 'Assets',
    meta: {title: '资产管理', icon: 'el-icon-office-building', roles: ['normal']},
    children: [
      {
        path: 'show',
        name: 'Show',
        component: () => import('@/views/myassets/show.vue'),
        meta: {title: '资产总览', icon: 'el-icon-s-data', roles: ['normal']}
      },
      {
        path: 'details',
        name: 'Details',
        component: () => import('@/views/myassets/details.vue'),
        meta: {title: '资产详情', icon: 'el-icon-document', roles: ['normal']}
      }
    ]
  },
  {
    path: '/mydebt',
    component: Layout,
    redirect: '/mydebt/catalog',
    name: 'Assets',
    meta: {title: '债权和债务', icon: 'el-icon-document-copy', roles: ['normal']},
    children: [
      {
        path: 'catalog',
        name: 'Catalog',
        component: () => import('@/views/mydebt/catalog.vue'),
        meta: {title: '分类展示', icon: 'el-icon-notebook-2', roles: ['normal']}
      },
      {
        path: 'details',
        name: 'Details',
        component: () => import('@/views/mydebt/details.vue'),
        meta: {title: '债权债务详情', icon: 'el-icon-tickets', roles: ['normal']}
      }
    ]
  },

  {
    path: '/usermanager',
    component: Layout,
    redirect: '/usermanager/list',
    name: 'UserManage',
    meta: {title: '用户管理', icon: 'el-icon-user', roles: ['admin']},
    children: [
      {
        path: 'list',
        name: 'List',
        component: () => import('@/views/users/list.vue'),
        meta: {
          title: '用户列表',
          icon: 'el-icon-files',
          roles: ['admin']
        }
      },
      {
        path: 'roles',
        name: 'Roles',
        component: () => import('@/views/users/roles.vue'),
        meta: {
          title: '角色管理',
          icon: 'el-icon-s-custom',
          roles: ['admin']
        }
      },
      {
        path: 'update',
        name: 'Update',
        component: () => import('@/views/users/editor.vue'),
        meta: {title: '用户资料编辑', icon: 'el-icon-s-custom'},
        hidden: true //控制菜单栏上是否隐藏
      },
      {
        path: 'details',
        name: 'Details',
        component: () => import('@/views/users/details.vue'),
        meta: {title: '用户资料详情', icon: 'el-icon-s-custom'},
        hidden: true //控制菜单栏上是否隐藏
      }
    ]
  },

  {
    path: '/settings',
    component: Layout,
    redirect: '/settings/system',
    name: 'Settings',
    meta: {title: '系统设置', icon: 'el-icon-user', roles: ['admin']},
    children: [
      {
        path: 'system',
        name: 'System',
        component: () => import('@/views/settings/system.vue'),
        meta: {
          title: '系统设置',
          icon: 'el-icon-setting',
          roles: ['admin']
        }
      }
    ]
  },

  {path: '*', redirect: '/404', hidden: true}
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({y: 0}),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
